package game_service

import (
	"GoServer/common"
	"GoServer/utils/databases"
	"context"
	"fmt"
)

// AddGiftScore 在收到礼物时根据对应游戏策略累加积分
func AddGiftScore(gameName, roomId string, giftValue uint64) {
	calc, ok := common.ScoreStrategyRegistry[gameName]
	if !ok {
		return
	}
	scoreToAdd := calc.Calculate(giftValue)
	key := fmt.Sprintf("score_pool:%s:%s", gameName, roomId)
	databases.Rediss.IncrBy(context.Background(), key, int64(scoreToAdd))
}

// GetCurrentAnchorScore 获取当前主播在指定游戏中的积分（不清空）
func GetCurrentAnchorScore(gameName, roomId string) uint64 {
	key := fmt.Sprintf("score_pool:%s:%s", gameName, roomId)
	val, err := databases.Rediss.Get(context.Background(), key).Uint64()
	if err != nil {
		return 0
	}
	return val
}

// SettleAnchorScore 游戏结束时结算并清空主播的积分池
func SettleAnchorScore(gameName, roomId string) uint64 {
	calc, ok := common.ScoreStrategyRegistry[gameName]
	if !ok {
		return 0
	}

	key := fmt.Sprintf("score_pool:%s:%s", gameName, roomId)
	val, err := databases.Rediss.GetDel(context.Background(), key).Uint64()
	if err != nil {
		return 0
	}
	return calc.Settlement(val)
}
